﻿using System;
using System.Collections.Generic;

namespace Domotica.Gazing.Calibration
{
    internal abstract class Processor
    {
        private readonly Calibrator _Calibrator;
        private readonly CalibrationState _State;
        private readonly ProcessList _Processes;
        private int _Current;

        protected Processor(Calibrator calibrator, 
            CalibrationState state)
        {
            _Calibrator = calibrator;
            _Processes = Initialize();
            _State = state;
            _Current = 0;
        }

        protected abstract ProcessList Initialize();

        public ProcessList Processes
        {
            get { return _Processes; }
        }

        public CalibrationState State
        {
            get { return _State; }
        }

        public Calibrator Calibrator
        {
            get { return _Calibrator; }
        }

        public bool Process()
        {
            while(_Current < _Processes.Count &&
                _Processes[_Current].DoProcess(_Calibrator))
            {
                _Current += 1;
            }

            return (_Current >= _Processes.Count);
        }

        public void OnFaceMove(GazeEventArgs e)
        {
            if (_Current >= _Processes.Count)
            {
                return;
            }
            _Processes[_Current].OnFaceMove(e);
        }

        public void OnGazeClick(GazeClickEventArgs e)
        {
            if (_Current >= _Processes.Count)
            {
                return;
            }
            _Processes[_Current].OnGazeClick(e);
        }

        public void OnGazeClickElapsed(GazeClickElapsedEventArgs e)
        {
            if (_Current >= _Processes.Count)
            {
                return;
            }
            _Processes[_Current].OnGazeClickElapsed(e, Calibrator);
         }
    }

    internal class ProcessorList : List<Processor>
    {}
}